package com.good.child.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.good.child.ui.utils.DimensionUtil;

/**
 * Create by xiaojie on 2024/8/25
 */
public class LineSoundEffectView extends View {

    private Paint paint;

    // dp转px的工具，自己网上找个
    public final int ticksWidth = DimensionUtil.dpToPx( 3);
    public final int dividerWidth =  DimensionUtil.dpToPx( 5);
    public final int ticksMaxHeight =  DimensionUtil.dpToPx(30);
    public final int ticksMinHeight = DimensionUtil.dpToPx( 6);
    public final int ticksNumber = 21;

    public final int fixOffset = (ticksMaxHeight - ticksMinHeight) / 2;

    // 计算总宽度
    public final int totalWidth = ticksWidth * ticksNumber + (dividerWidth * ticksNumber - 1);

    public LineSoundEffectView(Context context) {
        super(context);
        init();
    }

    public LineSoundEffectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineSoundEffectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setDither(true);
        offsetVertical = fixOffset;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private int offsetVertical = 0;

    private int totalNum = 21;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredWidth = totalWidth; // 假设我们想要设置的宽度为200px
        int desiredHeight = ticksMaxHeight; // 假设我们想要设置的高度为200px
        // 设置实际的宽度和高度
        setMeasuredDimension(desiredWidth, desiredHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < totalNum; i++) {

            int topY = offsetVertical;
            int bottomY = ticksMaxHeight - offsetVertical < ticksMinHeight ? ticksMinHeight :
                    ticksMaxHeight - offsetVertical;
            if (i < 4) {
                topY = fixOffset;
                bottomY = ticksMaxHeight - fixOffset;
                paint.setAlpha((int) ((i + 1) * 255 / 6f));
            } else if (i >= totalNum - 4) {
                topY = fixOffset;
                bottomY = ticksMaxHeight - fixOffset;
                paint.setAlpha((int) ((totalNum - i) * 255 / 5f));
            } else {
                // random 越大，音波越小
                int random = offsetVertical <= fixOffset ? i % 2 == 0 ? Math.abs(i < totalNum / 2 ? totalNum / 2 - i : i - totalNum / 2) : 0:Math.abs(i < totalNum / 2 ? totalNum / 2 - i : i - totalNum / 2)%5;
                int i1 = offsetVertical + random > fixOffset ? fixOffset : offsetVertical + random;
                topY = i1;
                bottomY = ticksMaxHeight - i1;
                paint.setAlpha(255);
            }
            canvas.drawRoundRect(new RectF((dividerWidth + ticksWidth) * i, topY, (dividerWidth + ticksWidth) * i + ticksWidth, bottomY), ticksWidth / 2, ticksWidth / 2, paint);
//            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            // 重绘视图
            invalidate();
            return true;
        }
        return super.onTouchEvent(event);
    }

    public void setEffect(int value, int maxValue) {
        offsetVertical = (int) (fixOffset * (1 - (value * 1f / maxValue)));
        invalidate();
    }
}